/**
 * Copyright(c) 2013-2015 by Puhuifinance Inc.
 * All Rights Reserved
 */
package com.puhui.chome.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.puhui.chome.controller.base.BaseController;
import com.puhui.chome.model.MonitorCc;
import com.puhui.chome.service.MonitorCcService;
import com.puhui.chome.util.StringUtil;

/**
 * @author dingxingang
 */
@Controller
@RequestMapping("/monitorCc")
public class MonitorCcController extends BaseController {

	@Autowired
	private MonitorCcService monitorCcService;

	/**
	 * 跳转列表页面
	 * 
	 * @author dingxingang
	 * @return 跳转地址
	 */
	@RequestMapping(value = "/toList")
	public String toList(ModelMap model, Long itemId) {
		model.put("itemId", itemId);
		return "monitorCc/monitorCcList";
	}

	/**
	 * 列表
	 * 
	 * @author dingxingang
	 * @param monitorCcVo
	 *            查询参数
	 * @param page
	 *            页码
	 * @param size
	 *            每页显示条数
	 * @param sort
	 *            排序
	 * @param order
	 *            正序or倒序
	 * @return 列表
	 */
	@ResponseBody
	@RequestMapping(value = "/monitorCcList", method = RequestMethod.POST)
	public Object monitorCcList(Long itemId, @RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "20") int size,
			@RequestParam(value = "sort", defaultValue = "id") String sort,
			@RequestParam(value = "order", defaultValue = "desc") String order) {
		// 查询条件
		Map<String, Object> param = new HashMap<>();
		param.put("itemId", itemId);
		// 查询总数
		long total = monitorCcService.findMonitorCcCount(param);
		int from = (page - 1) * size; // 计算开始条数
		// 查询数据
		param.put("from", from);
		param.put("size", size);
		param.put("sort", StringUtil.getFieldName(sort));
		param.put("order", order);
		List<MonitorCc> list = new ArrayList<>();
		if (total > 0) {
			list = monitorCcService.findMonitorCc(param);
		}
		// 返回信息
		Map<String, Object> rev = new HashMap<>();
		rev.put("total", total);
		rev.put("rows", list);
		return rev;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteMonitorCc", method = RequestMethod.POST)
	public Object deleteMonitorCc(Long id, ModelMap model) {
		monitorCcService.deleteMonitorCc(id);
		model.put("success", true);
		model.put("message", "操作成功");
		return model;
	}

	/**
	 * 修改
	 * 
	 * @author dingxingang
	 * @param monitorItem
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/upd")
	public Object upd(MonitorCc monitorCc, ModelMap model) {
		if (monitorCc != null) {
			Long id = monitorCc.getId();
			// 新增用户
			if (id == null) {
				monitorCcService.addMonitorCc(monitorCc);
			} else { // 修改用户
				monitorCcService.updateMonitorCc(monitorCc);
			}
		}
		model.put("monitorCc", monitorCc);
		model.put("message", "操作成功");
		return model;
	}

}
